<?php 

	Class LoginAction extends Action{
	public function index(){
		if(isset($_SESSION['username'])){
			$this->redirect('Index/index');
		}
		$this->display();
	}

	/**
	 * 生成验证码
	 */
	public function verify(){
		import('ORG.Util.Image');
		Image::buildImageVerify(6,1,'png',80,32);
	}

	/**
	 * 登陆处理
	 */
	public function CheckLogin(){
		$rand = mt_rand(time());
		if(!IS_POST)
		{
			$this->error('非法访问!');
		}else{
			if(I('passcode','','MD5')!=$_SESSION['verify'])
			{
				$this->error("验证码错误！",U('Admin/Index/index',array('rand'=>$rand)));
			}else{
				$username = I('admin');
				$password = I('password','','MD5');
				$user = M('user');
				$result = $user->where(array('username' => $username))->find();
				if($result['lock']==1) $this->error('用户被锁定，请联系管理员！',U('Admin/Index/index',array('rand'=>$rand)));
				if(!$result||$result['password']!=$password)
					{
						$this->error('登陆账号或密码错误！',U('Admin/Index/index',array('rand'=>$rand)));
					}else{
						//保存登陆ip和登陆时间
						$data = array(
							'username' => $username,
							'loginip' => get_client_ip(),
							'logintime' => time(),
							);
						M('user')->save($data);

						//设置session
						session('username',$result['username']);
						session('loginip',$result['loginip']);
						session('logintime',$result['logintime']);

						U('Index/index','','',true);//生成URL并跳转
					}
				
			}
		}
	}

	/**
	 * 注销登陆
	 */
	public function CheckOut(){
			session(null);
			$this->redirect('Login/index');
		}
}
 ?>